package com.occludens.admin.datamodel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Load;
import com.googlecode.objectify.annotation.Parent;
import com.googlecode.objectify.annotation.Subclass;

@Subclass(index=true)
@Cache
public class Type extends AdminObjectDataModel {
	@Parent private Ref<Type> derived;
	@Load private List<Ref<Attribute>> attributes = new ArrayList<Ref<Attribute>>();
	@Load private List<Ref<Trigger>> triggers = new ArrayList<Ref<Trigger>>();
	protected Type() {} // Mandatory for Objectify
	
	public Type(String name) {
		super(name);
	}
	
	public Type getDerived() {
		return this.derived.get();
	}
	
	public void setDerived (Type type) {
		this.derived = Ref.create(type);
	}
	
	public List<Attribute> getAttributes() {
		List<Attribute> returnList = new ArrayList<Attribute>();
		Iterator<Ref<Attribute>> attributesItr = this.attributes.iterator();
		while (attributesItr.hasNext()) {
			returnList.add(attributesItr.next().get());
		}//End of while attributesItr
		return returnList;
	}

	public void setAttributes(List<Attribute> attributes) {
		Iterator<Attribute> attributesItr = attributes.iterator();
		while (attributesItr.hasNext()) {
			Ref<Attribute> attributeRef = Ref.create(attributesItr.next());
			if (!this.attributes.contains(attributeRef)) {
				this.attributes.add(attributeRef);
			}
		}//End of while attributesItr
	}

	public void addAttributes(Attribute... attributes) {
		for (int i=0; i<attributes.length; i++) {
			Ref<Attribute> attributeRef = Ref.create(attributes[i]);
			if (!this.attributes.contains(attributeRef)) {
				this.attributes.add(attributeRef);
			}
		}
	}

	public void removeAttributes(Attribute... attributes) {
		for (int i=0; i<attributes.length; i++) {
			Ref<Attribute> attributeRef = Ref.create(attributes[i]);
			if (this.attributes.contains(attributeRef)) {
				this.attributes.remove(attributeRef);
			}
		}
	}
	
	public List<Trigger> getTriggers() {
		List<Trigger> returnList = new ArrayList<Trigger>();
		Iterator<Ref<Trigger>> triggersItr = this.triggers.iterator();
		while (triggersItr.hasNext()) {
			returnList.add(triggersItr.next().get());
		}//End of while triggersItr
		return returnList;
	}

	public void setTriggers(List<Trigger> triggers) {
		Iterator<Trigger> triggersItr = triggers.iterator();
		while (triggersItr.hasNext()) {
			Ref<Trigger> triggerRef = Ref.create(triggersItr.next());
			if (!this.triggers.contains(triggerRef)) {
				this.triggers.add(triggerRef);
			}
		}//End of while attributesItr
	}

	public void addTriggers(Trigger... triggers) {
		for (int i=0; i<triggers.length; i++) {
			Ref<Trigger> triggerRef = Ref.create(triggers[i]);
			if (!this.triggers.contains(triggerRef)) {
				this.triggers.add(triggerRef);
			}
		}
	}

	public void removeTriggers(Trigger... triggers) {
		for (int i=0; i<triggers.length; i++) {
			Ref<Trigger> triggerRef = Ref.create(triggers[i]);
			if (this.triggers.contains(triggerRef)) {
				this.triggers.remove(triggerRef);
			}
		}
	}
}
